//
//  HRHearRootTopView.m
//  Hear
//
//  Created by mac on 2017/11/22.
//  Copyright © 2017年 mac. All rights reserved.
//  头部话题

#import "HRHearRootTopView.h"
#import "HRUMShareTool.h"
@interface HRHearRootTopView ()<UITableViewDelegate,UITableViewDataSource>{
    CGFloat kCellWidth;
    CGFloat kCellHeight;
}

@end
@implementation HRHearRootTopView

-(void)setContentArr:(NSMutableArray *)contentArr{
    _contentArr = contentArr;
    self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
    [self.tableView reloadData];
    [self.tableView scrollToRow:self.currentIndex inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
/*
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        UIView *backgroundView = [[UIView alloc] init];
        backgroundView.layer.cornerRadius = 7;
        backgroundView.layer.masksToBounds = YES;
        [backgroundView setBackgroundColor:UIColorFromRGB(0xFFFFFF)];
        [self addSubview:backgroundView];
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.top);
            make.left.mas_equalTo(self.left);
            make.right.mas_equalTo(self.right);
            make.height.mas_equalTo(66 * scaleHeight);
        }];
        
        _topicLabel = [[UILabel alloc] init];
        _topicLabel.font = [UIFont systemFontOfSize:10*scaleWidth];
        _topicLabel.textAlignment = NSTextAlignmentLeft;
        _topicLabel.textColor = UIColorFromRGB(0xFFFFFF);
        _topicLabel.numberOfLines = 0;
        UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(entryTopicInfo)];
        tapLabel.numberOfTapsRequired = 1;       // 点击几次触发效果
        tapLabel.numberOfTouchesRequired = 1;    // 需要几个手指点击
        [self addSubview:_topicLabel];
        [_topicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(backgroundView.top).offset(12 * scaleHeight);
            make.left.mas_equalTo(backgroundView.left).offset(12 * scaleWidth);
            make.right.mas_equalTo(backgroundView.right).offset(12 * scaleWidth);
            make.bottom.mas_equalTo(backgroundView.bottom).offset(28 * scaleHeight);
        }];
        
        _shareTopicBtn = [UIButton buttonWithType:0];
        [_shareTopicBtn setImage:[UIImage imageNamed:@"转发1"] forState:0];
        [_shareTopicBtn addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_shareTopicBtn];
        [_shareTopicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_topicLabel.bottom).offset(8 * scaleHeight);
            make.right.mas_equalTo(_topicLabel.right).offset(0);
            make.bottom.mas_equalTo(backgroundView.bottom).offset(8 * scaleHeight);
            make.height.mas_equalTo(11 * scaleWidth);
        }];
        
        _answerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_answerBtn setBackgroundImage:[UIImage imageNamed:@"答"] forState:UIControlStateNormal];
        [_answerBtn addTarget:self action:@selector(answerTopic) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_answerBtn];
        [_answerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_topicLabel.bottom).offset(13 * scaleHeight);
            make.centerX.mas_equalTo(self);
            make.width.mas_equalTo(30 * scaleWidth);
            make.height.mas_equalTo(30 * scaleWidth);
        }];
        
    }
    return self;
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        
        self.backgroundColor = [UIColor whiteColor];
        

        
        kCellHeight = frame.size.height-50;
        
        CGFloat pading = 20;
        kCellWidth = kFullWidth-pading*2;
        
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, frame.size.width-20, frame.size.height-20)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        backView.backgroundColor = [UIColor whiteColor];
        backView.layer.shadowColor = [UIColor blackColor].CGColor;
        backView.layer.shadowRadius      = 3;
        backView.layer.shadowOpacity     = 0.2;
        backView.layer.shadowOffset      = CGSizeMake(1, 1);
        backView.layer.shadowPath        = [UIBezierPath bezierPathWithRect:backView.bounds].CGPath;
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsVerticalScrollIndicator = NO;
        //tableview逆时针旋转90度
        tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
        //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:(self.tableView=tableView)];
        tableView.pagingEnabled = YES;
        tableView.rowHeight = kFullWidth-pading*2;
        tableView.frame = CGRectMake(pading, pading, kCellWidth, kCellHeight);
        tableView.center = CGPointMake(kFullWidth / 2, kCellHeight / 2);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.centerY = backView.centerY;
        
        
 
        UIButton *lastButton = [UIButton buttonWithType:0];
        [lastButton setImage:[UIImage imageNamed:@"上一个-1"] forState:0];
        [lastButton addTarget:self action:@selector(lastButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:lastButton];
        lastButton.frame = CGRectMake(tableView.left+10, (tableView.height-20)/2+pading, 20, 20);
 
        
        UIButton *shareButton = [UIButton buttonWithType:0];
        [shareButton setImage:[UIImage imageNamed:@"转发1"] forState:0];
        [shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:shareButton];
        shareButton.frame = CGRectMake(lastButton.left, tableView.bottom-20, 20, 20);
        
 
        UIButton *nextButton = [UIButton buttonWithType:0];
        [nextButton setImage:[UIImage imageNamed:@"下一个-1"] forState:0];
        [nextButton addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:nextButton];
        nextButton.frame = CGRectMake(tableView.right-30, lastButton.top, lastButton.width, lastButton.height);
 
        
        UIButton *answerButton = [UIButton buttonWithType:0];
        [answerButton setImage:[UIImage imageNamed:@"答"] forState:0];
        [answerButton addTarget:self action:@selector(answerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:answerButton];
        answerButton.frame = CGRectMake((self.width-55)/2, self.height-55/2-pading, 55, 55);
            
        
        UILabel *numLabel = [UILabel new];
        numLabel.font = CustomUIFont(13);
        numLabel.textColor = SecondTextColor;
        [self  addSubview:(self.numLabel=numLabel)];
        numLabel.frame = CGRectMake(frame.size.width-50, self.frame.size.height-40, 50, 20);
        
        self.currentIndex = 0;
        
        
        
    }
    return self;
}


- (void)shareButtonClick{
    HRMainRootModel *model = self.contentArr[self.currentIndex];
    
    [HRUMShareTool uMShareToolTitle:@"又是平常而又唯一的一天，我有个秘密要告诉你。" andContentStr:[NSString stringWithFormat:@"你有%d个好友回答了这个问题，快来参加吧",arc4random() % 5] andImage:[UIImage imageNamed:@"hearicon"] andShareUrl:[NSString stringWithFormat:@"%@/M/SoundQuestionShare/%@",ShareHeadUrl,model.QuestionID]isSeccess:^(BOOL isSeccess) {
        
    }];
}

- (void)answerButtonClick{
    HRMainRootModel *model = self.contentArr[self.currentIndex];
    if (self.answerBlock){
        self.answerBlock(model.QuestionID,model.QuestionText);
    }
}

- (void)lastButtonClick{
    if (self.currentIndex == 0){
        [MBProgressHUD showMessage:@"当前话题是第一个" finishBlock:nil];
        return;
    }
    self.currentIndex--;
//    [self.tableView scrollToRow:self.currentIndex inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
    self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
    if (self.showCurrentAnswerPage){
        self.showCurrentAnswerPage(self.currentIndex);
    }
}

- (void)nextButtonClick{
    /*
    if (self.currentIndex == self.contentArr.count-1){
        self.currentIndex = 0;
        [self.tableView scrollToRow:0 inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
        self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
        if (self.showCurrentAnswerPage){
            self.showCurrentAnswerPage(self.currentIndex);
        }
        return;
    }
     */
    self.currentIndex++;

    if (self.showCurrentAnswerPage){
        self.showCurrentAnswerPage(self.currentIndex);
    }
//    [self.tableView scrollToRow:self.currentIndex inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
    self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
     
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentArr.count;
//    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HRHearRootTopCell *cell = [HRHearRootTopCell cellWithTableView:tableView];
    cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.contentArr.count){
        cell.model = self.contentArr[indexPath.row];
    }
    return cell;
}
/*
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    CGFloat currentPage = scrollView.contentOffset.y/kCellWidth;
    DDLog(@"==============scrollViewWillBeginDecelerating滑动了");

    if (currentPage > self.currentIndex){
        self.currentIndex++;
        if (self.currentIndex < self.contentArr.count){
            self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
            if (self.showCurrentAnswerPage){
                self.showCurrentAnswerPage(self.currentIndex);
            }
        }
    }else if (currentPage < self.currentIndex){
        self.currentIndex--;
        if (self.currentIndex >= 0){
            self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
            if (self.showCurrentAnswerPage){
                self.showCurrentAnswerPage(self.currentIndex);
            }
        }
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat currentPage = scrollView.contentOffset.y/kCellWidth;
    DDLog(@"==============scrollViewDidEndDecelerating滑动了");
    
    if (currentPage > self.currentIndex){
        self.currentIndex++;
        if (self.currentIndex < self.contentArr.count){
            self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
            if (self.showCurrentAnswerPage){
                self.showCurrentAnswerPage(self.currentIndex);
            }
        }
    }else if (currentPage < self.currentIndex){
        self.currentIndex--;
        if (self.currentIndex >= 0){
            self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.currentIndex+1,self.contentArr.count];
            if (self.showCurrentAnswerPage){
                self.showCurrentAnswerPage(self.currentIndex);
            }
        }

    }
}

*/



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}



@end
@interface HRHearRootTopCell ()
@property (nonatomic, strong)UILabel *titleLabel;
@end
@implementation HRHearRootTopCell

-(void)setModel:(HRMainRootModel *)model{
    self.titleLabel.text = model.QuestionText;
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"HRHearRootTopCell";
    
    HRHearRootTopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(!cell){
        cell = [[HRHearRootTopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.layer.cornerRadius = 12;
        self.clipsToBounds = YES;
        CGFloat pading = 40;

        UILabel *titleLabel = [UILabel new];
        titleLabel.font = CustomUIFont(15);
        titleLabel.textColor = FirstTextColor;
        [self.contentView  addSubview:(self.titleLabel=titleLabel)];
        titleLabel.frame = CGRectMake(pading, 0, kFullWidth-pading*3, adjuctFloat(100)-20);
        titleLabel.textAlignment = 1;
        titleLabel.numberOfLines = 0;
    }
    return self;
}

@end
